package com.lv.controller;

import com.lv.data.Result;
import com.lv.pojo.TbTypeTemplate;
import com.lv.service.TypeService;
import com.lv.util.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("type")
public class TypeController {
    @DubboReference
    private TypeService ts;

    @RequestMapping("totype")
    public String totype(){
        return "admin/type_template";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                          @RequestParam(defaultValue = "3") Integer pageSize){
        Page<TbTypeTemplate> pb =ts.getInfo(pageNum,pageSize);
        return Result.OK().setData(pb);
    }

    //增加
    @RequestMapping("add")
    @ResponseBody
    public Result add(TbTypeTemplate type){
        try {
            ts.add(type);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }

    //删除
    @RequestMapping("del")
    @ResponseBody
    public Result del(@RequestParam("ids") List<Long> ids){
        try {
            ts.del(ids);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }

    //回显
    @RequestMapping("toupdate/{id}")
    @ResponseBody
    public Result toupdate(@PathVariable("id") Long id){
        try {
            TbTypeTemplate toupdate = ts.toupdate(id);
            return Result.OK().setData(toupdate);
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public Result update(TbTypeTemplate type){
        try {
            ts.update(type);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }
}
